// -*- coding: utf-8 -*- 
/**
 * Project: LanQiao
 * Creator: yanking
 * Create time: 2022-02-20 14:45
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package Queen.eightQueen;

public class eightQueen {
    public int n = 8;
    public int count = 0;
    public int[][] map = new int[n][n];


    public void getCount() {

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                map[i][j] = 1;
            }
        }
        put(0);
        System.out.println(count);
    }

    public void put(int t) {
        if (t == 8) {
            count++;
            System.out.printf("This is the %d solution\n", count);
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    System.out.print(map[i][j] + "\t");
                }
                System.out.println();
            }

            return;
        }
        for (int i = 0; i < n; i++) {
            if (check(t, i)) {
                map[t][i] = 0;
            } else {
                // 当前位置不能放置皇后，判断当前行的下一个位置进行判断
                continue;
            }
            // 当前行防止完毕，防止下一行
            put(t + 1);
            map[t][i] = 1; // 回溯，不干扰下次放置
            // to check the situation here if can put a chess
            continue;
        }
    }

    public boolean check(int t, int i) {
        for (int q = t - 1; q >= 0; q--) {
            if (map[q][i] == 0) {
                return false;
            }
        }
        for (int q = t - 1, w = i - 1; q >= 0 && w >= 0; q--, w--) {
            if (map[q][w] == 0) {
                return false;
            }
        }
        for (int q = t - 1, w = i + 1; q >= 0 && w < 8; q--, w++) {
            if (map[q][w] == 0) {
                return false;
            }
        }
        return true;
    }
}
